<?php
class ClientAuthen {
	/**
     * The active user session, if one is available.
     */
	protected $session;
	/**
     * Indicates that we already loaded the session as best as we could.
     */
 	protected $sessionLoaded = false;
	/**
     * Indicates if Cookie support should be enabled.
     */
	protected static $_instance;
	private function  __construct() { 
		
	}
	
	/**
     *
     * Get instance of Ming_Authen
     * @return ClientAuthen
     */
    public static function getInstance(){
        if(null === self::$_instance){
            self::$_instance = new self();
        }
        return self::$_instance;
    }
	/**
	 * Is Session
	 * 
	 * @return boolean
	 */
	public function getSession() {
		if(!$this->sessionLoaded){
			$session = null;
      		$cookieName	=	$this->getSessionCookieName();
      		if (isset($_COOKIE[$cookieName])) {
      			$info	=	explode('-ming-cookie-', base64_decode( $_COOKIE[$cookieName] ));
      			$session['oauth_token']	=	$info[0];
      			$session['oauth_token_secret']	=	$info[1];
      			$session['user_id']	=	$info[2];
      			$session['type'] = $info[3];
      		}
      		$this->setSession($session);
		}
		return $this->session;
    }
    
    public function setSession($session){
    	if(isset($session['oauth_token']) && isset($session['oauth_token_secret']) && isset($session['user_id'])){
    		$this->sessionLoaded = true;
    		$this->setCookieFromSession($session);
    	}
    	$this->session	=	$session;
    	return $this;
    }
    
	/**
   	 * Set a JS Cookie based on the _passed in_ session. It does not use the
     * currently stored session -- you need to explicitly pass it in.
     *
     * @param Array $session the session to use for setting the cookie
     */
  	protected function setCookieFromSession($session=null) {
    	$expires	=	0;
  		$cookieName = 	$this->getSessionCookieName();
    	if ($session) {
    		if(!isset($session['type'])){//Type: 2 = facebook, 3 = twitter, 4 = google, 5 = yahoo
    			$session['type'] = '';
    		}
      		$value = base64_encode($session['oauth_token'].'-ming-cookie-'.$session['oauth_token_secret'].'-ming-cookie-'.$session['user_id'].'-ming-cookie-'.$session['type']);
    	}
    	if (headers_sent()) {
      		// disable error log if we are running in a CLI environment
      		// @codeCoverageIgnoreStart
      		if (php_sapi_name() != 'cli') {
        		error_log('Could not set cookie. Headers already sent.');
      		}
      		// @codeCoverageIgnoreEnd

    		// ignore for code coverage as we will never be able to setcookie in a CLI
    		// environment
    		// @codeCoverageIgnoreStart
    	}else{
    		setcookie($cookieName, $value, $expires, '/');
    	}
  	}
  	
	/**
     * The name of the Cookie that contains the session.
     *
     * @return String the cookie name
   	 */
  	protected function getSessionCookieName() {
    	return 'ming_cookie';
  	}
  	
	/**
	 * Set cookie to Browser
	 * 
	 */
	public function setCookie( $name, $value , $path='/' ){
		setcookie($name, $value, time()+ 24*3600*30, $path);
		$_COOKIE[$name] = $value;	  
	}
	
	/**
	 * Delete cookie
	 * 
	 */
	public function removeCookie( $name , $value , $path='/' ){
		setcookie($name, $value, time() - 24*3600*30, $path);
	}
	
	/**
	 * Get cookie from Browser
	 * 
	 */
	public function getCookie($name){
		if (isset($_COOKIE[$name])){
		  return $_COOKIE[$name]; 
		}
		return '';
	}
}

?>
